package com.jml.SimpleTomcat;



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Server2 {

	static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	public static void main(String[] args) {
		try {

			ServerSocket ss = new ServerSocket(8888);
			System.out.println("启动服务器....8888端口");

			while(true){

				Socket s = ss.accept();
				System.out.println("客户端:"+s.getInetAddress().getLocalHost()+"已连接到服务器");

				BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

				HttpRequest request = new HttpRequest( s.getInputStream() );

				String requesturi = request.getUri();

				System.out.println("请求路径是:"+requesturi);

				HttpResponse response = new HttpResponse(s.getOutputStream());


				//if(isStatic(requesturi)){
				response.writeFile(requesturi);
				//}

				s.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	//静态资源的重复获取需要通过过滤
	public static boolean isStatic( String uri ){

		boolean isStatic = false;

		String []suffixs = {"html","css","js","jpg","png","gif","bmp"};

		for( String suffix : suffixs){
			if(uri.endsWith("."+suffix)){
				isStatic = true;
				break;
			}
		}
		return isStatic;
	}

}

